﻿using System;
using System.Collections.Generic;

namespace BusinnesLogic.ServerObjects
{
    public abstract class Database
    {
        #region Ctor

        public Database(string databaseName)
        {
            this.Name = ParseDatabaseName(databaseName);
        }

        public Database(string serverName, string databaseName)
        {
            this.Name = ParseDatabaseName(databaseName);
        }

        #endregion

        #region Public Properties

        public string Name
        { get; internal set; }

        public int TableCount
        { get; internal set; }


        /// <summary>
        /// Get the database tables.
        /// </summary>
        public abstract List<Table> Tables
        { get; }

        /// <summary>
        /// Get the database tables with a specified index.
        /// </summary>
        /// <param name="index">position in array</param>
        /// <returns>Table</returns>
        public abstract Table this[int index]
        {
            get;
            set;
        }

        #endregion

        #region Public Methods

        public string ParseDatabaseName(string databaseName)
        {
            string name = string.Empty;

            // Remove [] branches
            foreach (char s in databaseName)
            {
                if (s != '[' && s != ']')
                    name += s;
            }

            // Remove dbo before the name
            if (name.StartsWith("dbo."))
                return name.Remove(0, 4);
            else
                return name;
        }

        #endregion
    }
}
